﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;
using System.Collections;

public partial class PayMonthBlance : ExtBaseRpt
{
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            txtmonth.Text = DateTime.Now.ToString("yyyy-MM-01");
            //BuildGridPanel(this.GridPanel1, mid, Session["UserID"].ToString());
        }
    }
    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        string str = "";
        if (txtshop.Text != "")
        {
            str += " and (shopcode like '%" + txtshop.Text + "%' or shopname like '%" + txtshop.Text + "%')";
        }
        if (txtmonth.Text != "" && Convert.ToDateTime(txtmonth.Text) != Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            str += " and paydate like '" + Convert.ToDateTime(txtmonth.Text).ToString("yyyy-MM") + "%'";
        }
        string sql = @"select * from V_CustomerMonthAcc where 1=1 " + str;
        this.Store1.DataSource = mydb.DM_GetDateSet(sql);
        this.Store1.DataBind();
    }
    protected void BtnDeletData(object sender, DirectEventArgs e)
    {
        if (txtmonth.Text == "" || Convert.ToDateTime(txtmonth.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            ShowBox("请选择删除日期！");
            return;
        }
        if (mydb.DM_GetDateSet("select * from KM_CustomerMonthAcc where  convert(varchar(7),paydate,23) ='" 
            + Convert.ToDateTime(txtmonth.Text).ToString("yyyy-MM") + "' and isnull(clstate,0)=1").Tables[0].Rows.Count > 0)
        {
            ShowBox("无法操作，客户已确认！");
            return;
        }
        string sql = "delete from KM_CustomerMonthAcc where convert(varchar(20),paydate,23) like '"
            + Convert.ToDateTime(txtmonth.Text).ToString("yyyy-MM") + "%'";
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            ShowBox("删除成功！");
            BtnQureyUser(null, null);
        }
        else
        {
            ShowBox("删除失败！");
        }
    }

    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null,null);
    }
    protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid,"xls",e.Xml,this.Response,this.Title);
    }
    [DirectMethod(Namespace = "CompanyX")]
    public void CwCheck(string shopcode, string paydate)
    {
        if (mydb.DM_GetDateSet("select *from KM_CustomerMonthAcc where shopcode='"
            + shopcode + "' and convert(varchar(7),paydate,23) ='" + paydate + "' and isnull(cwstate,0)=1").Tables[0].Rows.Count > 0)
        {
            ShowBox("无法操作，财务已确认！");
            return;
        }
        string sql = @"insert into KM_CustomerMonthAcc 
                        select shopcode,shopname,'"+paydate+@"-01',paymoney,fctot,fyamt,sjpay,sjfy,endamount,1,getdate(),0,null,'"+Session["UserCode"]+@"',getdate(),''
                        from V_CustomerMonthAcc where shopcode ='"+shopcode+"' and paydate ='"+paydate+"'";
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            ShowBox("操作成功！");
        }
        else
        {
            ShowBox("操作失败！");
        }
        BtnQureyUser(null, null);
    }
}